//
// Created by liushuai on 2021/1/17.
//

#include "treeRightView.h"
void treeRightView(TreeNode* root, std::vector<int>& result) {
  if (!root)
    return;
  else {
    std::queue<TreeNode*> nodes;
    if (root) nodes.push(root);
    int count = 0;
    std::vector<std::vector<int>> final;
    while (!nodes.empty()) {
      std::vector<int> layers;
      int node_size = nodes.size();
      for (int i = 0; i < node_size; i++) {
        TreeNode* current = nodes.front();
        nodes.pop();
        layers.push_back(current->val);
        if (current->left) nodes.push(current->left);
        if (current->right) nodes.push(current->right);
      }
      final.push_back(layers);
    }
    for (auto elem : final) {
      result.push_back(elem[elem.size() - 1]);
    }
  }
}
std::vector<int> treeRightView(std::vector<int>& a, std::vector<int>& b) {
  TreeNode* root = reConstructBinaryTree(a, b);
  std::vector<int> result;
  treeRightView(root, result);
  return result;
}
